home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks97 / KlingonBGApp.sit / Klingon BG App / Source / EventRoutines.c next >
Text File  |  1997-06-28  |  5KB  |  206 lines

  1. // Source code for Klingon Clock.   Copyright (C) 1996-1997
  2. // Charles H. Hemstreet IV
  3. //
  4. // Started at MacHack 1996
  5. // Completed at MacHack 1997
  6. //
  7. // Best thanks to:
  8. // My wife Regie, son Chad and baby
  9. // Other thanks to Elden Wood and Bob Clark
  10. //
  11. // This code is distributed "as-is" and implies no warranty or guarantee.
  12.  
  13.  
  14. #ifndef __EVENTROUTINES__
  15. #include "EventRoutines.h"
  16. #endif
  17.  
  18.  
  19. void DoDeallocateEverything(void)
  20. {
  21.     short    err;
  22.     
  23.     err = DoRemoveAEHandlers();
  24. }
  25.  
  26.  
  27. short DoInstallAEHandlers(void)
  28. {
  29.     short    err;
  30.     
  31.     err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
  32.                                  NewAEEventHandlerProc(myHandleQUIT), 0 ,false);
  33.     err = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
  34.                                  NewAEEventHandlerProc(myHandleRUN), 0 ,false);
  35.     err = AEInstallEventHandler(kTALKEventClass, kGetTALKEvent,
  36.                                  NewAEEventHandlerProc(myHandleTALK), 0 ,false);
  37.     err = AEInstallEventHandler(kTalkFREQEventClass, kGetTalkFREQEvent,
  38.                                  NewAEEventHandlerProc(myHandleTalkFREQ), 0 ,false);
  39.     return err;
  40. }
  41.  
  42.  
  43. short DoRemoveAEHandlers(void)
  44. {
  45.     short    err;
  46.     
  47.     err = AERemoveEventHandler(kCoreEventClass, kAEQuitApplication, 
  48.                                 NewAEEventHandlerProc(myHandleQUIT) ,false);
  49.     err = AERemoveEventHandler(kCoreEventClass, kAEOpenApplication, 
  50.                                 NewAEEventHandlerProc(myHandleRUN) ,false);
  51.     err = AERemoveEventHandler(kCoreEventClass, kGetTALKEvent, 
  52.                                 NewAEEventHandlerProc(myHandleTALK) ,false);
  53.     err = AERemoveEventHandler(kCoreEventClass, kGetTalkFREQEvent, 
  54.                                 NewAEEventHandlerProc(myHandleTalkFREQ) ,false);
  55.     return err;
  56. }
  57.  
  58.  
  59. void DoHandleQuitExtension(AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon)
  60. {
  61.     gExitNow = true;
  62. }
  63.  
  64.  
  65. void DoHandleTalkExtension(AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon)
  66. {
  67.     gExitNow = true;
  68. }
  69.  
  70.  
  71. void DoHighLevelEventExtension(const EventRecord *er)
  72. {
  73.     short    err;
  74.     err = AEProcessAppleEvent(er);
  75. }
  76.  
  77.  
  78. void GrabAndDoEvent(void)
  79. {
  80.     EventRecord        theEvent;
  81.     OSErr             myErr = noErr;
  82.  
  83.     if (WaitNextEvent(everyEvent, &theEvent, gSleepTicks, 0L))
  84.     {
  85.         DispatchEvent(&theEvent);
  86.     }
  87.     else
  88.     {
  89.         myErr = DoAllPeriodicProcessing();  // check the clock and tell the time if necessary
  90.         assert(myErr == noErr);
  91.     }
  92. }
  93.  
  94.  
  95. void DispatchEvent( EventRecord *macEvent)
  96. {
  97.     if (macEvent->what == 23) // High level event.
  98.     {
  99.         DoHighLevelEventExtension(macEvent);
  100.     }
  101. }
  102.  
  103.  
  104. // -----------------------------------------------------------------------------
  105. // -----------------------------------------------------------------------------
  106. // Below this point are support functions for the above routines.
  107.  
  108.  
  109. /******************************************************************************
  110.  myHandleQUIT
  111.  
  112. AE Handler for shut down.     
  113. ******************************************************************************/
  114.  
  115. pascal OSErr myHandleQUIT(AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon)
  116. {
  117.     DoHandleQuitExtension(theAppleEvent, reply, handlerRefcon);
  118.     
  119.     return noErr;
  120. }
  121.  
  122. /******************************************************************************
  123.  myHandleRUN
  124.  
  125. AE Handler for Open App.     
  126. ******************************************************************************/
  127.  
  128. pascal OSErr myHandleRUN(AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon)
  129. {
  130.  
  131.     return noErr;
  132. }
  133.  
  134. /******************************************************************************
  135.  myHandleTALK
  136.  
  137. AE Handler for clocktalk.     
  138. ******************************************************************************/
  139.  
  140. pascal OSErr myHandleTALK(AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon)
  141. {
  142.     OSErr myErr = noErr;
  143.     
  144.     myErr = DoAllPeriodicProcessing();
  145.     assert(myErr == noErr);
  146.  
  147.     return myErr;
  148. }
  149.  
  150.  
  151. /******************************************************************************
  152.  myHandleTalkFREQ
  153.  
  154. AE Handler for the clocks wait between talking...     
  155. ******************************************************************************/
  156.  
  157. pascal OSErr myHandleTalkFREQ(AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon)
  158. {
  159.     OSErr             myErr;
  160.     DescType         typeCode;
  161.     Size             sizeOfParam,
  162.                     actualSize;
  163.     long            myAEinterval = 1;
  164.  
  165.     myErr = AESizeOfParam(    theAppleEvent,
  166.                                keyDirectObject,
  167.                                &typeCode,
  168.                                &sizeOfParam);
  169.   
  170.     if(myErr != noErr){    
  171.         /*
  172.             If we fail here just return the error. We don't need to
  173.             do any clean up since we've allocated nothing on the heap
  174.             yet.  The Apple Event Manager automatically adds the error
  175.             number to the reply as keyErrorNumber for non zero handler
  176.             returns.
  177.         */
  178.         
  179.         return myErr;
  180.     }     
  181.     myErr = AEGetParamPtr(theAppleEvent, keyDirectObject, 
  182.                             typeLongInteger, &typeCode,
  183.                              (Ptr)&myAEinterval, sizeof(myAEinterval), 
  184.                             &actualSize);
  185.     
  186.  
  187.     if(myErr != noErr){    
  188.         /*
  189.             If we fail here just return the error. We don't need to
  190.             do any clean up since we've allocated nothing on the heap
  191.             yet.  The Apple Event Manager automatically adds the error
  192.             number to the reply as keyErrorNumber for non zero handler
  193.             returns.
  194.         */
  195.         
  196.         return myErr;
  197.     }     
  198.     gSleepTicks = 60*50*myAEinterval;  // nothing less than a minute please
  199.     
  200.     myErr = DoAllPeriodicProcessing();
  201.     assert(myErr == noErr);
  202.     
  203.     return noErr;
  204. }
  205.  
  206.